﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.App_Code;
using QXBook.Utils;
using System.Configuration;
using QXBook.Service;

namespace Web.SysManage.Admin
{
    public partial class MyAccount : AdminBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //检测管理员登录
            ServiceFactory.CreateInstance<AdminService>().CheckAdminLogin();
            ServiceFactory.CreateInstance<AdminService>().CheckAdminPop(9);

            if (!Page.IsPostBack)
            {
                this.iNickName.Value = base.adminInfo.NickName;
                this.adminname.Value = base.adminInfo.AdminName;
            }

            btnSave.Click += new EventHandler(btnSave_Click);
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void Save()
        {
            string adminname = CommonUtils.Post("adminname");
            string nickname = CommonUtils.Post("iNickName");
            string oldpwd = CommonUtils.Post("iOldPWD");
            string npwd = CommonUtils.Post("iNewPWD");

            if (string.IsNullOrEmpty(adminname) || string.IsNullOrEmpty(nickname))
            {
                base.ResponseErch("-1");
                return;
            }

            oldpwd = CommonUtils.MD5(oldpwd);
            if (!string.IsNullOrEmpty(npwd)) npwd = CommonUtils.MD5(npwd);

            string rtmessage = string.Empty;

            try
            {
                rtmessage = ServiceFactory.CreateInstance<AdminService>().ChanageAdminLoginInfo(adminname, oldpwd, npwd, nickname);
            }
            catch (Exception ex)
            {
                base.ajaxdata = "{state:false,message:\"" + CommonUtils.JSEncode(ex.Message.ToString()) + "\"}";
                base.ResponseErch(base.ajaxdata);
                return;
            }

            if (!string.IsNullOrEmpty(rtmessage))
            {
                SessionState.Remove(ConfigurationManager.AppSettings["AdminSessionName"]);
                CachingService.Remove(CachingService.CACHING_ALL_ADMIN_ENTITY);
            }

            base.ResponseErch(rtmessage, "密码修改成功！");
        }
    }
}